package leetcode.programmer;

// 面试题 10.01. 合并排序的数组
public class Merge {


    public void merge(int[] A, int m, int[] B, int n) {
        int p = m - 1;
        int q = n - 1;
        int index = A.length - 1;
        while (index >= 0 && q >= 0 && p >= 0) {
            if (A[p] > B[q]) {
                A[index--] = A[p];
                p--;
            } else {
                A[index--] = B[q];
                q--;
            }
        }
        // 把B里面剩下的放进去
        while (q >= 0) {
            A[index--] = B[q--];
        }

    }

    public void merge2(int[] A, int m, int[] B, int n) {
        int p = m - 1;
        int q = n - 1;
        int index = A.length - 1;
        while (index >= 0 && q >= 0) {
            if (p < 0 || B[q] > A[p]) {
                A[index--] = B[q--];
            } else {
                A[index--] = A[p--];
            }
        }
    }
}
